from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record
from tools.misc import UpdateableStr,UpdateableDict

Fields=UpdateableDict()
Forms=UpdateableStr()

class wizard_kerp_ar_3_months(wizard.interface):
    def _get_defaults(self, cr, uid, data, context={}):                
        Forms.string = '''<?xml version="1.0"?>
                <form string="Account Receivable">                            
                    <field name="quarter_lists" required="True"/>
                </form>'''
        cr.execute("""Select 
                        id,
                        name || to_char(date_start,': From dd-Mon-YYYY') || to_char(date_stop,' to dd-Mon-YYYY')
                    from 
                        account_period
                    order by
                        case when current_date between date_start and date_stop then 0 else 1 end,code desc;""")
        quarter_ids=[]
        list_quater=''
        default_quarter = False
        for id,quarter_name in cr.fetchall():
            if not default_quarter:
                default_quarter = id
            quarter_name=quarter_name.replace("'","\'")
            quarter_name=quarter_name.replace('"','\"')
            if list_quater:         
                list_quater = list_quater+",('"+str(id)+"','"+quarter_name+"')"
            else:
                list_quater = "('"+str(id)+"','"+quarter_name+"')"
           
        list_quater="["+list_quater+"]"
        Fields['quarter_lists']={'string':'Please select a Quarter','type':'selection','selection':eval(list_quater),'default': lambda *a:str(default_quarter)}
        
        return data['form']

    def _get_quarter_id(self, cr, uid, data, context={}):
        #quarter_id = int(data['form']['quarter_lists'])

        return {'model':'kdvn.payment.from.client', 'title' : 'Account Receivable  3 months'} 
        
    states = {
         'init': {
            'actions': [_get_defaults],
            'result': {
                'type': 'form',
                'arch':Forms,
                'fields':Fields,
                'state':[('end','Cancel','gtk-cancel'),('print_report','Print','gtk-print')]
            },
        },
        'print_report': {
            'actions': [_get_quarter_id],
            'result': {
                'type': 'print',
                'name' : 'Account Receivable 3 months',
                'report': 'kderp.report.account.receivable.three.months.hidden',
                'state': 'end'
            },
        }, 
    }
wizard_kerp_ar_3_months('jreports.kerp_ar_3_months')